home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / dskut / part.zip / PART.C < prev    next >
C/C++ Source or Header  |  1993-10-27  |  28KB  |  818 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <dos.h>
  5. #include <conio.h>
  6. #include <bios.h>
  7.  
  8. char version[] = "Version:  Mk 1.5     27 October 1993" ;
  9.  
  10. #define FALSE        0
  11. #define TRUE        1
  12. #define LOC_PT        0x1BE
  13. #define LOC_NT        0x1AA 
  14. #define LOC_GWR        0x1A9 
  15. #define MAGIC_LOC    0x1FE
  16. #define MAGIC_0        0x55
  17. #define MAGIC_1        0xAA
  18. #define NT_ENTRY_SIZE    5 
  19. #define SECSIZE        0x200
  20. #define sw_printf    if (sw_fancy) printf
  21.  
  22. struct disk_param {
  23.     unsigned int cyl ;
  24.     unsigned int head ;
  25.     unsigned int sect ;
  26. };
  27.  
  28. /* Global variables */
  29. int sw_help = FALSE, sw_null = FALSE, sw_number = FALSE ;
  30. int sw_bios = FALSE, sw_ide = FALSE, sw_details = FALSE, sw_fancy = TRUE ;
  31. unsigned int device = 0 ;
  32.  
  33. /* MAIN PROGRAM --- MAIN PROGRAM --- MAIN PROGRAM --- MAIN PROGRAM */
  34. int main (argc, argv)
  35. /***************************************************************/
  36. /*                                                             */
  37. /*  --- Partition Table and Hard Disk Examination Program ---  */
  38. /*                                                             */
  39. /*  Some code used by this program was derived from:           */
  40. /*      Pfdisk, Version 1.2.1 by Gordon W. Ross, Aug. 1990     */
  41. /*          and later modified by S. Lubkin  Oct. 1991         */
  42. /*      Dug_ide, Version 1.0 by Doug Merrett, 12 Jan 1993      */
  43. /*                                                             */
  44. /* Both programs like this program are public domain freeware. */
  45. /*                                                             */
  46. /*          (c) Copyright 1993 by Gary A. Allen, Jr.           */
  47. /*            Program written by Gary A. Allen, Jr.            */
  48. /*            Version:  Mk 1.5      27 October 1993            */
  49. /*                                                             */
  50. /***************************************************************/
  51.  
  52. int argc ;
  53. char *argv[] ;
  54. {
  55. register int i, j ;
  56.  
  57. int num_drv_bios, num_drv_act ;            /* Number of Hard disks */
  58. int useNTable;        /* boot sector uses name table */
  59. int cyl_bgn[4], cyl_lst[4], head_bgn[4], head_end[4] ;
  60. int sect_bgn[4], sect_end[4], type_code[4] ;
  61. int sw_active[4] ;
  62.  
  63. unsigned int dd[256];         /* Disk Data */
  64. unsigned int pbc,pbh,pbs;    /* physical beginning  c,h,s */
  65. unsigned int pec,peh,pes;    /* physical ending     c,h,s */
  66.  
  67. char chr, cmd[8] ;
  68. char *prt_nam[4], *output_string ;
  69. char buffer[SECSIZE];    /* The boot block buffer */
  70. char *active;
  71.  
  72. long sect_str[4], sect_lng[4] ;
  73.  
  74. unsigned long lsbeg,lslen;    /* logical sectors: begin, length */
  75.  
  76. static char fancy1[81] = {
  77. 0xc9,0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xd1,0xcd,0xcd,
  78. 0xcd,0xcd,0xd1,0xcd,0xcd,0xcd,0xcd,0xd1,0xcd,0xcd,
  79. 0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,
  80. 0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xd1,0xcd,
  81. 0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,
  82. 0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xd1,
  83. 0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xd1,
  84. 0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xbb,
  85. 0} ;
  86. static char fancy2[] =
  87.     "\xba Part \xb3" "Type\xb3" "Boot\xb3" "     Beginning"
  88.     "      \xb3" "      Ending        \xb3"
  89.     "Starting \xb3" "Number of\xba" ;
  90.  
  91. static char plain1[] =
  92.     "  Part  Type Boot      Beginning             Ending         "
  93.     "Starting  Number of\n" ;
  94.  
  95. static char fancy3[] =
  96.     "\xba" " Type \xb3" "Code\xb3" "Part\xb3" "Side Cylinder Sector\xb3"
  97.     "Side Cylinder Sector\xb3" 
  98.     " Sector  \xb3" " Sectors \xba";
  99.  
  100. static char plain2[] =
  101.     "  Type  Code Part Side Cylinder Sector "
  102.     "Side Cylinder Sector  Sector    Sectors \n";
  103.  
  104. static char fancy4[81] = {
  105.     0xcc,0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xd8,0xcd,0xcd,
  106.     0xcd,0xcd,0xd8,0xcd,0xcd,0xcd,0xcd,0xd8,0xcd,0xcd,
  107.     0xcd,0xcd,0xd1,0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,
  108.     0xcd,0xd1,0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xd8,0xcd,
  109.     0xcd,0xcd,0xcd,0xd1,0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,
  110.     0xcd,0xcd,0xd1,0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xd8,
  111.     0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xd8,
  112.     0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xb9,
  113. 0} ;
  114.  
  115. static char fancy5[] =
  116. "\xba" "%6.6s\xb3" "%#4x\xb3" "%4.4s\xb3" "%3u \xb3" "%7u \xb3" "%5u \xb3"
  117. "%3u \xb3" "%7u \xb3" "%5u \xb3" "%8lu \xb3" "%8lu \xba" ;
  118.  
  119. static char plain3[] =
  120. " %6.6s %#4x %4.4s %3u  %7u  %5u  %3u  %7u  %5u  %8lu  %8lu \n" ;
  121.  
  122. static char fancy6[81] = {
  123.     0xc7,0xc4,0xc4,0xc4,0xc4,0xc4,0xc4,0xc5,0xc4,0xc4,
  124.     0xc4,0xc4,0xc5,0xc4,0xc4,0xc4,0xc4,0xc5,0xc4,0xc4,
  125.     0xc4,0xc4,0xc5,0xc4,0xc4,0xc4,0xc4,0xc4,0xc4,0xc4,
  126.     0xc4,0xc5,0xc4,0xc4,0xc4,0xc4,0xc4,0xc4,0xc5,0xc4,
  127.     0xc4,0xc4,0xc4,0xc5,0xc4,0xc4,0xc4,0xc4,0xc4,0xc4,
  128.     0xc4,0xc4,0xc5,0xc4,0xc4,0xc4,0xc4,0xc4,0xc4,0xc5,
  129.     0xc4,0xc4,0xc4,0xc4,0xc4,0xc4,0xc4,0xc4,0xc4,0xc5,
  130.     0xc4,0xc4,0xc4,0xc4,0xc4,0xc4,0xc4,0xc4,0xc4,0xb6,
  131. 0} ;
  132.  
  133. static char fancy7[81] = {
  134.     0xc8,0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xcf,0xcd,0xcd,
  135.     0xcd,0xcd,0xcf,0xcd,0xcd,0xcd,0xcd,0xcf,0xcd,0xcd,
  136.     0xcd,0xcd,0xcf,0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,
  137.     0xcd,0xcf,0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xcf,0xcd,
  138.     0xcd,0xcd,0xcd,0xcf,0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,
  139.     0xcd,0xcd,0xcf,0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xcf,
  140.     0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xcf,
  141.     0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xcd,0xbc,
  142. 0} ;
  143.  
  144. struct part {    /* An entry in the partition table */
  145.     unsigned char    active;        /* active flag (0x80 or 0) */
  146.     unsigned char    b_head;        /* begin head */
  147.     unsigned char    b_sect ;    /*      sector */
  148.     unsigned char    b_cyl;        /*     cylinder */
  149.     unsigned char    sysid;        /* system id */
  150.     unsigned char    e_head;        /* end  head */
  151.     unsigned char    e_sect ;    /* end    sector */
  152.     unsigned char    e_cyl;        /* end    cylinder */
  153.     /* These two are just longs, but this way is machine independent. */
  154.     unsigned long    lsBeg;        /* logical sectors, beginning */
  155.     unsigned long    lsLen;        /* logical sectors, length */
  156. } *pp ;    /* partition table entry */
  157.  
  158. struct disk_param dk, lg, le ;
  159.  
  160. /* Function Prototype */
  161. int getGeometry(int, struct disk_param*) ;
  162. int getBBlk(int, char*) ;
  163. char *getascii(unsigned int in_data[], int off_start, int off_end);
  164. char *nameID(unsigned int);
  165. void listPTable(void) ;
  166. void long2chs(unsigned long, struct disk_param*, struct disk_param*) ;
  167. void switch_list(void) ; 
  168. void cmd_switch(char, int, char *[]) ;
  169. void bugs(void), help(void) ;
  170.  
  171. /* Print heading */
  172. printf("          ") ;
  173. printf("  --- Partition Table and Hard Disk Examination Program ---\n") ;
  174. printf("                    ") ;
  175. printf("  Program written by Gary A. Allen, Jr.          \n") ;
  176. printf("                    ") ;
  177. printf("  %s\n",version) ;
  178. printf("                    ") ;
  179. printf("(c) Copyright 1993 by Gary A. Allen, Jr.\n\n") ;
  180.  
  181. /* Scan the after the dos command PART.EXE for switches or file name */
  182. if (argc > 1) {
  183. for (i = 1; i <= argc-1; i++) {
  184.     /* Flush command vector */
  185.     for (j = 0; j <= 7; j++) cmd[j] = '\0' ;
  186.     /* Get command vector */
  187.     sscanf(argv[i],"%c%c%c%c%c%c%c%c%c", &chr, &cmd[0], &cmd[1],
  188.         &cmd[2], &cmd[3], &cmd[4], &cmd[5], &cmd[6], &cmd[7]) ;
  189.     sw_null = FALSE ;
  190.     if (chr == '?') help() ;
  191.     if (chr == '-') {
  192.         for (j = 0; j <= 7; j++) {
  193.             cmd_switch(cmd[j], i, argv) ;
  194.             if (sw_number) {
  195.                 i++ ;
  196.                 sw_number = FALSE ;
  197.                 break ;
  198.             }
  199.             if (sw_null) break ;
  200.         } /* end of command "for" loop */
  201.  
  202.         if (j >= 7) {
  203. printf("\n!!!! Too many commands on one \"-\" switch group.\n") ;
  204. printf("!!!! Break up the commands into several \"-\" switch groups.\n") ;
  205.             bugs() ;
  206.         }
  207.     } /* end of the "-" marker block */
  208.  
  209.     /* Deal with garbage on the command line */
  210.     else {
  211. printf ("\n!!!! The following on the command line was not understood:  %s \n",
  212.     argv[i]);
  213.         bugs() ;
  214.     } /* garbage block */
  215.  
  216. } /* end of argument token "for" loop */
  217.  
  218. if (sw_help) help() ;
  219. } /* end of the command switch block */
  220.  
  221. /* determine actual number of disk drives */
  222. num_drv_act = 0 ;
  223. while (!getGeometry(num_drv_act++,&dk)) ;
  224. num_drv_act-- ;
  225.  
  226. if (num_drv_act == 0) {
  227. printf("!!!! There is no hard disk on this computer!\n") ;
  228.     bugs() ;
  229. }
  230.  
  231. /* State the drive number */
  232. if (num_drv_act != 1) printf("Describing hard disk #%d\n", device);
  233.  
  234. /* Ask BIOS how many disk drives there are */
  235. /* Byte at Segment 40H Offset 75H */